package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.BlogUserSettings;

public class GenericBlogUserSettingsDAO implements
		org.jetnuke.dao.BlogUserSettingsDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericBlogUserSettingsDAO.class);

	public BlogUserSettings retrieveByPk(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPk(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BlogUserSettingsModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		BlogUserSettings s = new BlogUserSettings();

		if (rs.next()) {
			fillBlogUserSettingsFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPk(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	protected void fillBlogUserSettingsFromResultSet(BlogUserSettings s,
			ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillBlogUserSettingsFromResultSet(BlogUserSettings, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setID(rs.getInt(("ID")));
		s.setTITLE(rs.getString("TITLE"));
		s.setSTYLE(rs.getInt("THEMES_ID"));
		s.setMAXNUMBERPOST(rs.getInt("MAX_NUMBER_POSTS"));
		s.setBLOCKCONTENT1(rs.getString("BLOCK_CONTENT_1"));
		s.setBLOCKTITLE1(rs.getString("BLOCK_TITLE_1"));
		s.setBLOCKCONTENT2(rs.getString("BLOCK_CONTENT_2"));
		s.setBLOCKTITLE2(rs.getString("BLOCK_TITLE_2"));
		s.setBLOCKCONTENT3(rs.getString("BLOCK_CONTENT_3"));
		s.setBLOCKTITLE3(rs.getString("BLOCK_TITLE_3"));
		s.setBLOCKCONTENT4(rs.getString("BLOCK_CONTENT_4"));
		s.setBLOCKTITLE4(rs.getString("BLOCK_TITLE_4"));
		s.setBLOCKCONTENT5(rs.getString("BLOCK_CONTENT_5"));
		s.setBLOCKTITLE5(rs.getString("BLOCK_TITLE_5"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillBlogUserSettingsFromResultSet(BlogUserSettings, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	public void update(BlogUserSettings bs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(BlogUserSettings) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogUserSettingsModel.update"));
		p.setString(1, bs.getDESCRIPTION());
		p.setInt(2, bs.getMAXNUMBERPOST());
		p.setInt(3, bs.getSTYLE());
		p.setString(4, bs.getTITLE());
		p.setString(5, bs.getBLOCKTITLE1());
		p.setString(6, bs.getBLOCKCONTENT1());
		p.setString(7, bs.getBLOCKTITLE2());
		p.setString(8, bs.getBLOCKCONTENT2());
		p.setString(9, bs.getBLOCKTITLE3());
		p.setString(10, bs.getBLOCKCONTENT3());
		p.setString(11, bs.getBLOCKTITLE4());
		p.setString(12, bs.getBLOCKCONTENT4());
		p.setString(13, bs.getBLOCKTITLE5());
		p.setString(14, bs.getBLOCKCONTENT5());
		p.setInt(15, bs.getID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(BlogUserSettings) - end"); //$NON-NLS-1$
		}
	}

	public void insert(BlogUserSettings bs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("insert(BlogUserSettings) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogUserSettingsModel.insert"));
		p.setString(1, bs.getDESCRIPTION());
		p.setInt(2, bs.getMAXNUMBERPOST());
		p.setInt(3, bs.getSTYLE());
		p.setString(4, bs.getTITLE());
		p.setInt(5, bs.getID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("insert(BlogUserSettings) - end"); //$NON-NLS-1$
		}
	}

}
